Advertisement
Guest User

Untitled

a guest
Mar 15th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. //Random -function, timer method
  2. //
  3. //Timer_A is setup in capture mode. SMCLK is set to the
  4. //DCO and set as the input clock to Timer_A. ACLK
  5. //is set to the VLO, which is the trigger for the capture.
  6. //Timer_A counts the number of DCO clock pulses
  7. //before the next VLO low-to-high transition occurs.
  8. //The number of DCO clock pulses is saved by the timer
  9. //in a Capture/Compare Register (CCR).
  10.  
  11. int randomize_led(){
  12.  
  13.     while(1){
  14.        
  15.     BCSCTL2 &= ~(DIVS1 + DIVS0);            // SMCLK / 1 (subsystem clock)
  16.  
  17.     TACCR0 = 0;
  18.    
  19.     TACCTL0 = CAP | CM_1 | CCIS_1;            // Capture mode, positive edge/ aclk input
  20.     TACTL = TASSEL_2 | MC_2;                  // SMCLK, continuous up
  21.  
  22.    
  23.     while (!(CCIFG & TACCTL0));       // Wait for interrupt
  24.  
  25.     TACCTL0 &= ~CCIFG;                // Clear interrupt
  26.  
  27.     result =  TACCR0;        
  28.    
  29.     result = result & 0x0007;               // bit masking the result to 3 bits
  30.    
  31.                
  32.     BCSCTL2 |= DIVS1 + DIVS0;               // SMCLK / 8 (subsystem clock)
  33.                                             // set SMCLK back to 125KHZ
  34.    
  35.     if(result > 6){
  36.        
  37.         result = 6;
  38.            
  39.     }
  40.                
  41.     if(result < 1){
  42.         result = 1;
  43.     }
  44.                
  45.         if(result != prev_result){
  46.             prev_result = result;
  47.             return result;
  48.            
  49.         }
  50.    
  51.     }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement